<%@ page language="java" import="java.util.*,com.hs.auth.model.*,com.hs.auth.*" pageEncoding="utf-8"%>
<%
HsAccount accInfo = (HsAccount)request.getSession().getAttribute(AUTHConstant.USERINSESSION);
HsUserinfo userinfo = accInfo==null?null:accInfo.getUserinfo();
String accid = String.valueOf(accInfo==null?"":accInfo.getId()); 
String treeStr3 = (String)request.getAttribute("treeStr3");  
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<%@ include file="../../include.jsp"%> 
<link rel="stylesheet" href="<%=basePath %>thirtpart/jquery/ztree/css/demo.css" type="text/css"/>
<link rel="stylesheet" href="<%=basePath %>thirtpart/jquery/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css"/>
<script type="text/javascript" src="<%=basePath %>thirtpart/jquery/ztree/js/jquery.ztree.core-3.1.js"></script>
<script type="text/javascript" src="<%=basePath %>thirtpart/jquery/ztree/js/jquery.ztree.excheck-3.1.js"></script>
<script type="text/javascript" src="<%=basePath %>thirtpart/jquery/ztree/js/jquery.ztree.exedit-3.1.js"></script>
<script src='<%=basePath%>dwr/interface/dataHelper.js'></script>  
<style type="text/css">
.ztree li button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle}
</style>
<script language=JavaScript>
  	<!--
		var setting = {
			view: {
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom,
				selectedMulti: false
			},
			edit: {
				enable: true,
				editNameSelectAll: true
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeDrag: beforeDrag,
				beforeEditName: beforeEditName,
				beforeRemove: beforeRemove,
				beforeRename: beforeRename,
				onRemove: onRemove,
				onRename: onRename
			}
		};

		var zNodes =<%=treeStr3%>;
		var log, className = "dark";
		function beforeDrag(treeId, treeNodes) {
			return false;
		}
		function beforeEditName(treeId, treeNode) {
			className = (className === "dark" ? "":"dark"); 
			var zTree = $.fn.zTree.getZTreeObj("tree1");
			zTree.selectNode(treeNode);
			return true;
		}
		function beforeRemove(treeId, treeNode) {
			className = (className === "dark" ? "":"dark"); 
 			var zTree = $.fn.zTree.getZTreeObj("tree1");
			zTree.selectNode(treeNode); 
			var sNodes = zTree.getSelectedNodes();
			if (sNodes.length > 0) {
				var isParent = sNodes[0].isParent;
			}
			if(isParent){
				alert("请先删除该节点下的子节点!");
				return false;
			}
			
			if(5 == treeNode.id){
			   alert("方剂分类根节点不能删除!");
			   return false;
			}else{
			    if(confirm("您确认要删除该分类节点吗?")){
			        
			 		dataHelper.delTag(treeNode.id,<%=accid%>,1,delCallBack);
			 	}else{
			 		return false;
			 	}
			}
			return true;
		}
		function onRemove(e, treeId, treeNode) { 
			
			//showLog("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
		}
		function delCallBack(data){
			if("" != data){
				alert("删除成功!");
 			}else{
				alert("删除失败!");
			}
		}
		function beforeRename(treeId, treeNode, newName) { 
			var nameold = treeNode.name;
			className = (className === "dark" ? "":"dark");
 			if (newName.length == 0) {
				alert("节点名称不能为空.");  
				var treeObj = $.fn.zTree.getZTreeObj("tree1"); 
				treeObj.cancelEditName(nameold); 
				//setTimeout(function(){zTree.editName(treeNode)}, 10); 
				return false;
			}else{
				dataHelper.updateTag(treeNode.id,newName,nameold,<%=accid%>,updateCallBack); 
				return true;
			}
			return true;
		}
		function updateCallBack(data){ 
			if("" == data){
				alert("分类名称修改成功!");
 			} else{
 			    alert("分类名称重复！");
 				var treeObj = $.fn.zTree.getZTreeObj("tree1");  
 				var nodeid = data.substring(data.indexOf('#')+1,data.length);
 				var nodename = data.substring(0,data.indexOf('#')); 
 			    var node = treeObj.getNodeByParam("id", nodeid, null); 
 			    if(null != node){
					node.name = nodename;
					treeObj.updateNode(node); 
				}
 			}
		}
		function onRename(e, treeId, treeNode) {
			//showLog("[ "+getTime()+" onRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
		}
		function showLog(str) {
			if (!log) log = $("#log");
			log.append("<li class='"+className+"'>"+str+"</li>");
			if(log.children("li").length > 8) {
				log.get(0).removeChild(log.children("li")[0]);
			}
		}
		function getTime() {
			var now= new Date(),
			h=now.getHours(),
			m=now.getMinutes(),
			s=now.getSeconds(),
			ms=now.getMilliseconds();
			return (h+":"+m+":"+s+ " " +ms);
		}

		var newCount = 1;
		var treenode1 = null;
		function addHoverDom(treeId, treeNode) {
		    var nodename = "新中药分类" + (newCount++);
			var sObj = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0) return;
			var addStr = "<button type='button' class='add' id='addBtn_" + treeNode.id
				+ "' title='新增分类' onfocus='this.blur();'></button>";
			sObj.append(addStr);
			var btn = $("#addBtn_"+treeNode.id);
			treenode1 = treeNode;
			if (btn) btn.bind("click", function(){
				
				dataHelper.addTag(nodename,treeNode.id,<%=accid%>,3,addCallBack);
				
				return false;
			});
			
		};
		function addCallBack(data){
			if("" != data){
				var arr = data.split('#');
				var zTree = $.fn.zTree.getZTreeObj("tree1");
				zTree.addNodes(treenode1, {id:arr[0], pId:treenode1.id, name:arr[1]});
				alert("新增中药分类成功!");
			}
		}
		function removeHoverDom(treeId, treeNode) {
			$("#addBtn_"+treeNode.id).unbind().remove();
		};
		function selectAll() {
			var zTree = $.fn.zTree.getZTreeObj("tree1");
			zTree.setting.edit.editNameSelectAll =  $("#selectAll").attr("checked");
		}
		
		$(document).ready(function(){
			$.fn.zTree.init($("#tree1"), setting, zNodes);
			$("#selectAll").bind("click", selectAll);
		});
		//-->
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr> 
        <TD>&nbsp;</TD>
        <td>
         <div  style="width:100%" id="herbdiv">
			<ul id="tree1" class="ztree" style="width:98%"></ul>
	     </div>
        </td> 
        <TD>&nbsp;</TD>
    </tr>
</table>
</body>
</html>
<script language=javascript>
var MS = "IE";
if(navigator.userAgent.indexOf("Firefox") > 0) {
    MS = "FF";
}
if (document.getElementById("herbdiv")!=null) {   
     if (document.documentElement.clientHeight > 200) {
        document.getElementById("herbdiv").style.height = document.documentElement.clientHeight-25 + "px";
     }
}

</script>